package com.fruit.service.picture.impl;

import java.io.File;
import java.io.FileInputStream;
import java.io.InputStream;
import java.io.OutputStream;
import java.util.HashMap;
import java.util.Map;

import org.joda.time.DateTime;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Service;
import org.springframework.web.multipart.MultipartFile;

import com.fruit.service.pic.PictureManager;
import com.fruit.util.FtpUtil;
import com.fruit.util.IDUtils;
import com.fruit.util.PicUtil;


/**
 * 图片上传服务
 * 
 * @author Administrator
 *
 */
@Service("PictureService")
public class PictureSevice implements PictureManager {

	@Value("${FTP_ADDRESS}")
	private String FTP_ADDRESS;
	@Value("${FTP_PORT}")
	private Integer FTP_PORT;
	@Value("${FTP_USERNAME}")
	private String FTP_USERNAME;
	@Value("${FTP_PASSWORD}")
	private String FTP_PASSWORD;
	@Value("${FTP_BASE_PATH}")
	private String FTP_BASE_PATH;
	@Value("${IMAGE_BASE_URL}")
	private String IMAGE_BASE_URL;
	@Value("${FTP_IMAGE_BASE_PATH}")
	private String FTP_IMAGE_BASE_PATH;

	@SuppressWarnings("rawtypes")
	@Override
	public Map uploadPicture(String strBase64, String suffix) {
		Map resultMap = new HashMap<>();
		try {
			// 生成新文件名
			String newName = IDUtils.genImageName();
			newName = newName + suffix;
			// 图片上传
			String imagePath = new DateTime().toString("/yyyy/MM/dd");
			
			InputStream in = PicUtil.base64ToInputStream(strBase64, false);
			boolean result = FtpUtil.uploadFile(FTP_ADDRESS, FTP_PORT, FTP_USERNAME, FTP_PASSWORD, FTP_IMAGE_BASE_PATH,
					imagePath, newName, in);
			// 返回结果
			if (!result) {
				resultMap.put("error", 1);
				resultMap.put("message", "文件上传失败");
				return resultMap;
			}
			resultMap.put("error", 0);
			resultMap.put("url", IMAGE_BASE_URL + imagePath + "/" + newName);
			return resultMap;

		} catch (Exception e) {
			resultMap.put("error", 1);
			resultMap.put("message", "文件上传发生异常");
			return resultMap;
		}
	}

	@Override
	public Map uploadPicture(String strBase64, String path, String name,boolean isCut) {
		Map resultMap = new HashMap<>();
		try {
			InputStream in = PicUtil.base64ToInputStream(strBase64, isCut);
			boolean result = FtpUtil.uploadFile(FTP_ADDRESS, FTP_PORT, FTP_USERNAME, FTP_PASSWORD, FTP_IMAGE_BASE_PATH,
					path, name, in);
			// 返回结果
			if (!result) {
				resultMap.put("error", 1);
				resultMap.put("message", "文件上传失败");
				return resultMap;
			}
			resultMap.put("error", 0);
			resultMap.put("url", IMAGE_BASE_URL + path + "/" + name);
			return resultMap;

		} catch (Exception e) {
			resultMap.put("error", 1);
			resultMap.put("message", "文件上传发生异常");
			return resultMap;
		}
	}
	@Override
	public Map uploadPictureFile(InputStream in, String path, String name) {
		Map resultMap = new HashMap<>();
		try {
			boolean result = FtpUtil.uploadFile(FTP_ADDRESS, FTP_PORT, FTP_USERNAME, FTP_PASSWORD, FTP_IMAGE_BASE_PATH,
					path, name, in);
			// 返回结果
			if (!result) {
				resultMap.put("error", 1);
				resultMap.put("message", "文件上传失败");
				return resultMap;
			}
			resultMap.put("error", 0);
			resultMap.put("url", IMAGE_BASE_URL + path + "/" + name);
			return resultMap;

		} catch (Exception e) {
			resultMap.put("error", 1);
			resultMap.put("message", "文件上传发生异常");
			return resultMap;
		}
	}

	@Override
	public Map<String, Object> uploadPicture(InputStream in, String end, int width, int height, float quality) {
		Map<String, Object> resultMap = new HashMap<String, Object>();
		try {
			// 生成新文件名
			// UUID.randomUUID();
			String newName = IDUtils.genImageName();
			newName = newName + "_"+end+".jpg";
			// 图片上传
			String imagePath = new DateTime().toString("/yyyy/MM/dd");
			boolean result = FtpUtil.uploadFile(FTP_ADDRESS, FTP_PORT, FTP_USERNAME, FTP_PASSWORD, FTP_BASE_PATH,
					imagePath, newName, in,width, height, quality);
			// 返回结果
			if (!result) {
				resultMap.put("error", 1);
				resultMap.put("message", "文件上传失败");
				return resultMap;
			}
			resultMap.put("error", 0);
			resultMap.put("url", IMAGE_BASE_URL + imagePath + "/" + newName);
			return resultMap;

		} catch (Exception e) {
			resultMap.put("error", 1);
			resultMap.put("message", "文件上传发生异常");
			return resultMap;
		}

	}
	
	


}
